package com.quanyan.travel.service.impl;

import com.alibaba.dubbo.common.json.JSON;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists;
import com.google.gson.Gson;
import com.quanyan.api.APIResponse;
import com.quanyan.travel.api.req.ReqSeasonActivities;
import com.quanyan.travel.constants.OutdoorConstants;
import com.quanyan.travel.entity.*;
import com.quanyan.travel.entity.vo.*;
import com.quanyan.travel.mapper.*;
import com.quanyan.travel.service.TravelListService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 场馆内部处理Service
 * Created by yangyun on 2016/4/19.
 */



@Service("travelListService")
public class TravelListServiceImpl implements TravelListService {

    private static Logger logger = LoggerFactory.getLogger(TravelListServiceImpl.class);

    @Autowired
    TbOutdoorActivityMapper outdoorActivityMapper;

    @Autowired
    TbOutdoorDiscountMapper outdoorDiscountMapper;

    @Autowired
    TbOutdoorProductMapper outdoorProductMapper;

    @Autowired
    private VTravelUserDefinedMapper vTravelUserDefinedMapper;

    @Autowired
    TbOutdoorThemeMapper outdoorThemeMapper;


    @Override
    public APIResponse<SeasonActivity> seasonActivieies(ReqSeasonActivities requet) {
        SeasonActivity seasonActivity = new SeasonActivity();
        ObjectMapper mapper = new ObjectMapper();
        //查询活动基本信息和活动折扣信息
        TbOutdoorActivityExample tbOutdoorActivityExample = new TbOutdoorActivityExample();
        TbOutdoorActivityExample.Criteria outdoorActivityCriteria = tbOutdoorActivityExample.createCriteria();
        outdoorActivityCriteria.andIdEqualTo(requet.getActivityId());
        outdoorActivityCriteria.andIsDeleteEqualTo(OutdoorConstants.IS_NOT_DELETE);
        TbOutdoorActivity outdoorActivity = outdoorActivityMapper.selectByPrimaryKey(requet.getActivityId());
        if (null == outdoorActivity) {
            return APIResponse.returnFail("查询季度活动为空！");
        } else {
            seasonActivity.setActivityId(requet.getActivityId());
            try {
                MultiMedia multiMedia = mapper.readValue(outdoorActivity.getMultiMedia(),MultiMedia.class);
                seasonActivity.setMultiMedia(multiMedia);
            } catch (IOException e) {
                logger.info("活动id={}的多媒体格式错误！",requet.getActivityId());
            }
            //查询折扣
            if (null != outdoorActivity) {
                TbOutdoorDiscountExample outdoorDiscountExample = new TbOutdoorDiscountExample();
                TbOutdoorDiscountExample.Criteria outdoorDiscountCriteria = outdoorDiscountExample.createCriteria();
                outdoorDiscountCriteria.andActivityIdEqualTo(requet.getActivityId());
                outdoorActivityCriteria.andIsDeleteEqualTo(OutdoorConstants.IS_NOT_DELETE);
                List<TbOutdoorDiscount> tbOutdoorDiscounts = outdoorDiscountMapper.selectByExample(outdoorDiscountExample);
                if (null != tbOutdoorDiscounts && tbOutdoorDiscounts.size() > 0) {
                    TbOutdoorDiscount tbOutdoorDiscount = tbOutdoorDiscounts.get(0);
                    Discount discount = new Discount();
                    BeanUtils.copyProperties(tbOutdoorDiscount, discount);
                    List<DiscountParm> list = Lists.newArrayList();
                    try {
                        list = mapper.readValue(tbOutdoorDiscount.getDiscountParameter(), List.class);
                        discount.setDiscountDetail(list);
                    } catch (IOException e) {
                        logger.info("活动id={}的折扣格式错误！",tbOutdoorDiscount.getActivityId());
                    }
                    seasonActivity.setDiscount(discount);
                }
            }
            //查询场次
            TbOutdoorThemeExample tbOutdoorThemeExample = new TbOutdoorThemeExample();
            TbOutdoorThemeExample.Criteria themeExampleCriteria = tbOutdoorThemeExample.createCriteria();
            themeExampleCriteria.andActivityIdEqualTo(requet.getActivityId());
            themeExampleCriteria.andIsDeleteEqualTo(OutdoorConstants.IS_NOT_DELETE);
            List<TbOutdoorTheme> tbOutdoorThemes = outdoorThemeMapper.selectByExample(tbOutdoorThemeExample);
            if (tbOutdoorThemes != null && tbOutdoorThemes.size() > 0) {
                List<Integer> productids = Lists.newArrayList();
                for (TbOutdoorTheme theme : tbOutdoorThemes) {
                    productids.add(theme.getProductId());
                }
                List<SeasonActivityVo> seasonActivityVos = vTravelUserDefinedMapper.querySeasonActivityList(productids);
                for(SeasonActivityVo seasonActivityVo : seasonActivityVos){
                    try {
                        MultiMedia multiMedia = mapper.readValue(seasonActivityVo.getMultiMediaStr(),MultiMedia.class);

                        Integer day  = Math.round(seasonActivityVo.getActivity_time() / 24);
                        if(day == 0){
                            day = 1;
                        }
                        seasonActivityVo.setDayNum(String.valueOf(day));
                        seasonActivityVo.setMultiMedia(multiMedia);
                        List<DestinationVo> destinationVos = vTravelUserDefinedMapper.queryDestination(seasonActivityVo.getId());
                        seasonActivityVo.setDestinationList(destinationVos);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                seasonActivity.setActivities(seasonActivityVos);
            }
            /*TbOutdoorProductExample outdoorProductExample = new TbOutdoorProductExample();
            TbOutdoorProductExample.Criteria outdoorProductCriteria = outdoorProductExample.createCriteria();
            outdoorProductCriteria.andIsDeleteEqualTo(OutdoorConstants.IS_NOT_DELETE);
            outdoorProductCriteria.andActivityIdEqualTo(requet.getActivityId());
            List<TbOutdoorProduct> products = outdoorProductMapper.selectByExample(outdoorProductExample);
            if (products != null && products.size() > 0) {
                List<Integer> integerList = Lists.newArrayList();
                for (TbOutdoorProduct tbOutdoorProduct : products) {
                    integerList.add(tbOutdoorProduct.getActivityId());
                }
                TbOutdoorActivityExample outdoorActivityExample = new TbOutdoorActivityExample();
                TbOutdoorActivityExample.Criteria criteria = outdoorActivityExample.createCriteria();
                criteria.andIdIn(integerList);
                List<TbOutdoorActivity> productDetails = outdoorActivityMapper.selectByExample(outdoorActivityExample);
                List<BaseActivity> baseActivities = new ArrayList<BaseActivity>();
                if (null != productDetails && productDetails.size() > 0) {
                    for (TbOutdoorActivity tbOutdoorActivity : productDetails){
                        BaseActivity baseActivity = new BaseActivity();
                        List<DestinationVo> destinationVos = vTravelUserDefinedMapper.queryDestination(tbOutdoorActivity.getId());
                        baseActivity.setDestinationList(destinationVos);
                        BeanUtils.copyProperties(tbOutdoorActivity,baseActivity);
                        try {
                            MultiMedia multiMedia = mapper.readValue(tbOutdoorActivity.getMultiMedia(),MultiMedia.class);
                            baseActivity.setMultiMedia(multiMedia);
                        }catch (Exception e){
                        }
                         baseActivities.add(baseActivity);
                    }
                    seasonActivity.setBaseActivities(baseActivities);
                }
            }*/
            return APIResponse.returnSuccess(seasonActivity);
        }
    }
















}

    /*
    @Autowired
    VOutdoorActivityPreviewMapper outdoorActivityPreviewMapper;
    @Autowired
    TbOutdoorDiscountMapper discountMapper;

    @Override
    public APIResponse<BaseSeasonActivities> seasonActivieies(ReqSeasonActivities requet) {

        //查询季度活动父级
        BaseSeasonActivities result = new BaseSeasonActivities();
        SeasonActivity seasonActivity = new SeasonActivity();
        List<SeasonActivity> list = Lists.newArrayList();
        VOutdoorActivityPreviewExample activityPreviewExample = new VOutdoorActivityPreviewExample();
        VOutdoorActivityPreviewExample.Criteria activityPreviewCriteria = activityPreviewExample.createCriteria();
        activityPreviewCriteria.andIdEqualTo(requet.getParentId());
        List<VOutdoorActivityPreview> outdoorActivityPreviews = outdoorActivityPreviewMapper.selectByExample(activityPreviewExample);
        if(outdoorActivityPreviews != null && outdoorActivityPreviews.size() > 0){
            BeanUtils.copyProperties(outdoorActivityPreviews.get(0),seasonActivity);
            Gson gson = new Gson();
            MultiMedia multiMedia = gson.fromJson(outdoorActivityPreviews.get(0).getMultiMedia(), MultiMedia.class);
            seasonActivity.setMultiMedia(multiMedia);
        }
        result.setSeasonActivity(seasonActivity);

        VOutdoorActivityPreviewExample activityPreviewExample1 = new VOutdoorActivityPreviewExample();
        VOutdoorActivityPreviewExample.Criteria activityPreviewCriteria1 = activityPreviewExample1.createCriteria();
        int parentid = requet.getParentId();
        activityPreviewCriteria1.andParentIdEqualTo((byte)parentid);
        List<VOutdoorActivityPreview> outdoorActivityPreviews1 = outdoorActivityPreviewMapper.selectByExample(activityPreviewExample1);
        if(null != outdoorActivityPreviews1&&outdoorActivityPreviews1.size() >0 ) {
            for(VOutdoorActivityPreview outdoorActivityPreview:outdoorActivityPreviews1){
                SeasonActivity s = new SeasonActivity();
                BeanUtils.copyProperties(outdoorActivityPreview,s);
                Gson gson = new Gson();
                MultiMedia multiMedia = gson.fromJson(outdoorActivityPreview.getMultiMedia(), MultiMedia.class);
                s.setMultiMedia(multiMedia);
                list.add(s);
            }
            result.setActivityItems(list);
        }

        TbOutdoorDiscountExample outdoorDiscountExample = new TbOutdoorDiscountExample();
        TbOutdoorDiscountExample.Criteria outdoorDiscountCriteria = outdoorDiscountExample.createCriteria();
        outdoorDiscountCriteria.andActivityIdEqualTo(requet.getParentId());
        List<TbOutdoorDiscount> tbOutdoorDiscounts = discountMapper.selectByExample(outdoorDiscountExample);
        if(null != tbOutdoorDiscounts && tbOutdoorDiscounts.size() > 0){
            TbOutdoorDiscount tbOutdoorDiscount = tbOutdoorDiscounts.get(0);
            ActivityDiscount activityDiscount = new ActivityDiscount();
            BeanUtils.copyProperties(tbOutdoorDiscount,activityDiscount);
            String discount = tbOutdoorDiscount.getDiscountParameter();
            Gson gson = new Gson();
            List<DiscountParameter> discountParameters = Lists.newArrayList();
            discountParameters = gson.fromJson(discount,List.class);
            activityDiscount.setDiscountParameter(discountParameters);
            result.setDiscount(activityDiscount);
        }
        return APIResponse.returnSuccess(result);
    }
}
*/
